// <license>
//  Exception4Net is exception handler framework for dot net.
//  Copyright (C) 2002  sijingnan
//  
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// </license>
namespace Exception4Net 
{
	using System;
	using System.Resources;
	using System.Configuration;
	using Exception4Net.Config;

	/// <summary>
	/// Contains helper functions for exception.
	/// </summary>
	/// <remarks>
	/// 	created by - si jingnan
	/// 	created on - 2002-9-11 15:48:25
	/// </remarks>
	public class ExceptionHelper 
	{
		private String resourcePath = null;
		private String resourceName = null;
		private ResourceManager rm = null;

		private static readonly ExceptionHelper instance = new ExceptionHelper();
		
		/// <summary>
		/// Default constructor - initializes all fields to default values
		/// </summary>
		private ExceptionHelper() 
		{
		}
	
		///<summary>
		/// return the default resource manager for exception
		/// <remarks>
		/// The Resource
		/// </remarks>
		/// </summary>
		public ResourceManager DefaultResourceManager 
		{
			get
			{
				if (rm == null)
				{
					//if no resource manager set
					//check config path
					if (this.resourcePath == null)
					{
						//config path is null
						//check the default on in application settings.
						object config = ConfigurationSettings.GetConfig(Exception4NetConfigurationSectionHandler.CONFIGURATION_TAG);

						if (config is Exception4NetConfigurationSectionHandler)
						{
							Exception4NetConfigurationSectionHandler handler = 
								(Exception4NetConfigurationSectionHandler)config;

							resourcePath = handler.ResourcePath;
							resourceName = handler.ResourceName;
						}

						if ((resourcePath == null) || "".Equals(resourcePath.Trim()) ||
							(resourceName == null) || "".Equals(resourceName.Trim()))
						{
							//no resource specified
							return null;
						}
						else
						{
							try
							{
								rm = ResourceManager.CreateFileBasedResourceManager(resourceName,
									resourcePath,null);
							}
							catch
							{
								//create resource manager fail.
								return null;

							}
						}

						return rm;
					}
					else
					{
						return rm;
					}
				}
				else
				{
					return rm;
				}
			}
		}

		/// <summary>
		/// return the single instance of Exception Helper
		/// </summary>
		public static ExceptionHelper Instance
		{
			get
			{
				return instance;
			}
		}

		/// <summary>
		/// Represent the exception4net's resource path.
		/// </summary>
		public String ResourcePath
		{
			get
			{
				return this.resourcePath;
			}
			set
			{
				this.resourcePath = value;
			}
		}

		/// <summary>
		/// Represent the exception4net's resource base name.
		/// </summary>
		public String ResourceName
		{
			get
			{
				return this.resourceName;
			}
			set
			{
				this.resourceName = value;
			}
		}
	}
}
